public class BinaryTreeSortDemo {
    public static void main(String[] args) {
        int[] array = {7, 3, 10, 12, 5, 1, 9, 0};
        BinaryTree binaryTree = new BinaryTree();

        for(int i = 0; i < array.length; i++){
            binaryTree.add(new Node(array[i]));
        }

        System.out.println("中序遍历二叉排序树");
        binaryTree.infixOrder();

//        Node node = binaryTree.searchParent(9);
//        System.out.println(node);

        System.out.println("删除结点：");
        binaryTree.delNode(10);
        binaryTree.infixOrder();


    }
}

//创建二叉树
class BinaryTree{
    private Node root;

    public Node getRoot() {
        return root;
    }

    public void add(Node node){
        if(root == null){
            root = node;
        }else{
            root.add(node);
        }
    }

    public void infixOrder(){
        if(root != null){
            root.infixOrder();
        }else{
            System.out.println("二叉树为空无法排序");
        }
    }

    public Node search(int value){
        if(root == null){
            return null;
        }else{
            return root.search(value);
        }
    }

    public Node searchParent(int value){
        if(root == null){
            return null;
        }else{
            return root.searchParent(value);
        }
    }

    /**
     * 找到目标节点下的子树的最小值节点
     * @param node:目标节点
     * @return：返回目标节点下子树的节点的最小值
     */
    public int delRightTreeMin(Node node){
        Node target = node;

        //一直向左找可以找到最小值
        while(target.left != null){
            target = target.left;
        }
        delNode(target.value);
        return target.value;
    }

    public void delNode(int value){
        if(root == null){
            return;
        }else{
            Node targetNode = search(value);
            if(targetNode == null){
                return;
            }
            if(root.left == null && root.right == null){
                root = null;
                return;
            }

            //去找父节点
            Node parent = searchParent(value);
            //如果目标节点为叶子结点
            if(targetNode.left == null && targetNode.right == null){
                if(parent.left != null && parent.left.value == value){
                    parent.left = null;
                }else if(parent.right != null && parent.right.value == value){
                    parent.right = null;
                }
            }else if(targetNode.left != null && targetNode.right != null){//目标节点有两个子节点
                int minValue = delRightTreeMin(targetNode.right);
                targetNode.value = minValue;
            }else{//目标节点只有一个子节点

                //当目标节点只有左节点时
                if(targetNode.left != null){
                    if(parent != null) {
                        //当目标节点值等于父节点的左节点时
                        if (parent.left.value == value) {
                            //目标节点左节点赋给父节点的左节点
                            parent.left = targetNode.left;
                        } else {
                            //目标节点左节点赋给父节点的右节点
                            parent.right = targetNode.left;
                        }
                    }else{
                        root = targetNode.left;
                    }
                }else{//当目标节点只有右节点时
                    if(parent != null) {
                        //当目标节点值等于父节点的右节点时
                        if (parent.right.value == value) {
                            //目标节点右节点赋给父节点的右节点
                            parent.right = targetNode.right;
                        } else {
                            //目标节点右节点赋给父节点的左节点
                            parent.left = targetNode.right;
                        }
                    }else{
                        root = targetNode.right;
                    }
                }
            }
        }
    }

}

//创建节点
class Node{
    int value;
    Node left;
    Node right;

    public Node(int value) {
        this.value = value;
    }


    /**
     * 寻找目标节点
     * @param value:目标节点的值
     * @return:返回目标节点
     */
    public Node search(int value){
        if(value == this.value){
            return this;
        } else if(value < this.value){
            if(this.left == null){
                return null;
            }else{
                return this.left.search(value);
            }
        }else {
            if(this.right == null){
                return null;
            }else{
                return this.right.search(value);
            }
        }
    }

    /**
     * 寻找目标节点的父节点
     * @param value:目标节点的值
     * @return:返回目标节点父节点
     */
    public Node searchParent(int value){
        if((this.right != null && this.right.value == value) || (this.left != null && this.left.value == value)){
            return this;
        }else {
            if(this.value > value && this.left != null){
                return this.left.searchParent(value);
            }else if(this.value <= value && this.right != null){
                return this.right.searchParent(value);
            }else{
                return null;
            }
        }
    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }

    public void add(Node node){
        if(node == null){
            return;
        }
        if(node.value < this.value){
            if(this.left == null){
                this.left = node;
            }else{
                this.left.add(node);
            }
        }else{
            if(this.right == null){
                this.right = node;
            }else{
                this.right.add(node);
            }
        }

    }

    public void infixOrder(){
        if(this.left != null){
            this.left.infixOrder();
        }

        System.out.println(this);

        if(this.right != null){
            this.right.infixOrder();
        }
    }
}

